package gui.mainView;

import gui.advancedView.UIAdvancedView;
import gui.passwordView.UIPasswordView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import commons.Manager;
import commons.exceptions.ProcessException;

public class UIControler implements ActionListener {

	Manager manager;
	UIView uiView;

	public UIControler(UIView uiView, Manager manager){
		this.manager = manager;
		this.uiView = uiView;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(uiView.play)){
			if(!manager.isPlaying()){
				try {
					manager.play();
				} catch (ProcessException e1) {
				}
			}else{
				try {
					manager.pause();
				} catch (ProcessException e1) {
				}
			}
		}else if(e.getSource().equals(uiView.stop)){
			manager.stopMedia();
		}else if(e.getSource().equals(uiView.next)){
			try {
				manager.next();
			} catch (ProcessException e1) {
			}
		}else if(e.getSource().equals(uiView.controls)){
			UIPasswordView.getInstance(uiView).kill();
			UIAdvancedView uiAdvView = UIAdvancedView.getInstance(uiView);
			if(uiAdvView!=null && !uiView.advancedOpened){
				uiView.getContentPane().add(uiAdvView);
				uiView.advancedOpened=true;
			}
		}else if(e.getSource().equals(uiView.parameters)){
			UIAdvancedView.getInstance(uiView).kill();
			UIPasswordView uiPasswdView = UIPasswordView.getInstance(uiView);
			if(uiPasswdView!=null && !uiView.settingsOpened){
				uiView.getContentPane().add(uiPasswdView);
				uiView.settingsOpened=true;
			}
		}
	}

}
